package a630wj.recycleview630wj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import a630wj.recycleview630wj.R;
import a630wj.recycleview630wj.bean.CourseBean;

/**
 * Created by weijie on 2017/6/9.
 */

public class CourseAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<CourseBean> list;
    private OnItemClickListener mOnItemClickListener;

    public CourseAdapter(Context context,List<CourseBean>list){
        this.context=context;
        this.list=list;
    }
    public void setList(List<CourseBean>list){
        this.list=list;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context)
                .inflate(R.layout.item, parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder1, int position) {
        final int mPostion= position;
        CourseBean bean=list.get(position);
        ViewHolder holder=(ViewHolder)holder1;
        holder.tv_title.setText(bean.getKcmc());
        holder.tv_descreption.setText(bean.getKcdm());
        holder.tv_time.setText(bean.getInsert_time()+"");
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"+bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(holder.imageView);

        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //int mposition = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(view, mPostion);
                    //OnItemClickListener(view,mPostion);
                }});
        }
    }

    @Override
    public int getItemCount() {
        if (list!=null)
            return list.size();
        else
            return 0;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tv_title,tv_descreption,tv_time;
        Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tv_title = (TextView) itemView.findViewById(R.id.textView);
            tv_descreption = (TextView) itemView.findViewById(R.id.textView2);
            tv_time = (TextView) itemView.findViewById(R.id.textView3);
            button = (Button) itemView.findViewById(R.id.button3);
        }
    }
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }


    public void setOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener=listener;
    }
}
